#include "stdafx.h"
#include "MyChannel.h"
#include "MyQuery.h"
#include "MyConfig.h"


//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
void CMyChannel::_LoopLoadIDb(ISLDbGroup* pIGroup,CMyNode* pNode)
{
	int nAttrVT = 0,nAttrData = 0,nAttrBuffLen = 4;

	if(pNode != m_pDbRoot)
	{
		pNode->m_csName = pIGroup->GetName();
		pNode->m_csDesc = pIGroup->GetDesc();
	}
	pNode->m_pIDbGroup = pIGroup;
	m_lstAllNodes.push_back(pNode);

	/*
	CMyNode* pTagParentNode = pNode;
	if(pNode == m_pDbRoot)
	{
		pTagParentNode = m_pDbSystem;
		m_pDbSystem->m_pIDbGroup = pIGroup;
		m_lstAllNodes.push_back(m_pDbSystem);
	}
	*/

	ISLDbTagArray* pIDbTagArray = pIGroup->GetITagArray();
	for(ISLDbTagArray::iterator it = pIDbTagArray->begin(); it != pIDbTagArray->end(); it++)
	{
		ISLDbTag* pIDbTag = (ISLDbTag*)*it;

		if(pIDbTag->GetTypeID() == ST_TAG_HMITAG)
		{
			if(!m_pMyConfig->m_uFlag.bLoadSysTags)
				continue;
		}
		if(pIDbTag->GetTypeID() == ST_TAG_FIELD)
		{
			if(!m_pMyConfig->m_uFlag.bLoadIoFields)
				continue;
			if(m_pMyConfig->m_uFlag.bLoadOnlyIoStatus)
			{
				if(pIDbTag->GetName().compare("IoStatus") != 0)
					continue;
			}
		}

		CMyTag* pMyTag = new CMyTag(pNode,m_pIApp);
		pNode->m_lstTags.push_back(pMyTag);
		m_lstAllTags.push_back(pMyTag);

		pMyTag->m_csName = pIDbTag->GetName();
		pMyTag->m_csDesc = pIDbTag->GetDesc();
		pMyTag->m_nTypeId = pIDbTag->GetTypeID();
		pMyTag->m_pIDbTag = pIDbTag;

		pIDbTag->GetAttrib(0,nAttrVT,nAttrBuffLen,(unsigned char*)&nAttrData);
		pMyTag->m_nVT = nAttrData;
		pIDbTag->GetAttrib(1,nAttrVT,nAttrBuffLen,(unsigned char*)&nAttrData);
		pMyTag->m_nAccess = nAttrData;

		//
		//std::string csNamePath = pIDbTag->GetName(1);
		//std::transform(csNamePath.begin(),csNamePath.end(),csNamePath.begin(), ::tolower); 
		//std::pair<CMyTagMap::iterator, bool> prResult = m_mapMyTags.insert(std::make_pair(csNamePath, pMyTag));

	}

	ISLDbGroupArray* pIDbGroupArray = pIGroup->GetSubGroupArray();
	for(ISLDbGroupArray::iterator it = pIDbGroupArray->begin(); it != pIDbGroupArray->end(); it++)
	{
		ISLDbGroup* pISubGroup = (ISLDbGroup*)*it;

		CMyNode* pMySubNode = new CMyNode(pNode,m_pIApp);
		pNode->m_lstSubNodes.push_back(pMySubNode);
		_LoopLoadIDb(pISubGroup,pMySubNode);
	}
}

void CMyChannel::_CreateDsTags()
{
	std::string csDsName,csDsDesc,csLinker;
	for(CMyNodeList::iterator itNode = m_lstAllNodes.begin(); itNode != m_lstAllNodes.end(); itNode++)
	{
		CMyNode* pNode = (CMyNode*)*itNode;

		for(CMyTagList::iterator itTag = pNode->m_lstTags.begin(); itTag != pNode->m_lstTags.end(); itTag++)
		{
			CMyTag* pMyTag = (CMyTag*)*itTag;

			pMyTag->m_csNamePath = BN_StringFormat("%s.%s",pMyTag->m_pParent->GetName(1).c_str(),pMyTag->m_csName.c_str());
			csDsName = pMyTag->m_pIDbTag->GetName(2);
			csLinker = pMyTag->m_pIDbTag->GetName(1);
			csDsDesc = pMyTag->m_pIDbTag->GetDesc(1);
			pMyTag->m_pIDsTag = m_pIChannel->AddDSTag(csDsName,csDsDesc,csLinker,pMyTag->m_nVT,1000);
			pMyTag->m_pIDsTag->GetPrivateData()->n[0] = (FST_INT_PTR)pMyTag;
			pMyTag->m_pIDsTag->SetAttribute(0,&pMyTag->m_nAccess,4);
			pMyTag->m_pIDsTag->SetNotify(1);
		}
	}
}

//////////////////////////////////////////////////////////////////////////
//
//////////////////////////////////////////////////////////////////////////
int CMyChannel::OnEventOccur(int nType, void* pIEvent, IDsTag* pITag)
{
	if(pIEvent == NULL)
		return 0;
	TSLIEvent* pEventSrc = (TSLIEvent*)pIEvent;

	TSLIEvent* pEvent = new TSLIEvent;
	*pEvent = *pEventSrc;
	m_lstEvent.push_back(pEvent);

	if(m_lstEvent.size() > 1024)
	{
		delete m_lstEvent.front();
		m_lstEvent.pop_front();
	}

	return 0;
}

int CMyChannel::OnRTDbNotified(TSYM_VALUENOTIFIED* pValue, IDsTag* pITag)
{
	if(pITag == NULL)
		return 0;
	CMyTag* pMyTag = (CMyTag*)pITag->GetPrivateData()->n[0];
	if(pMyTag == NULL)
		return 0;

	pITag->ResetValueChangedCounter();
	TZData* pZData = new TZData;
	pZData->pMyTag = pMyTag;
	pZData->tTime = pValue->tTime;
	pZData->nQuality = pValue->nQuality;
	pZData->nVT = pValue->nVT;
	switch (pValue->nVT)
	{
	case SVT_INT:
		{
			pZData->nValue = pValue->lValue;
		}
		break;
	case SVT_INT64:
		{
			pZData->nValue64 = pValue->nValue64;
		}
		break;
	case SVT_DOUBLE:
		{
			pZData->dbValue = pValue->dbValue;
		}
		break;
	case SVT_STRING:
		{
			pZData->csValue = *(pValue->csValue);
		}
		break;
	default:
		{
			delete pZData;
			return 0;
		}
		break;
	}
	m_pZPoolRecv->AddObject(pZData,true);
	return 0;
}



int CMyChannel::OnIoWriteReturn(TIO_WRITEDATA* pIoWriteResult, IDsTag* pITag)
{

	return 0;
}


